//
//  TagCell.swift
//  HelloTagListView
//
//  Created by Chen,Meisong on 2020/11/7.
//

import UIKit
import SnapKit

class TagCell: UICollectionViewCell {
    // MARK: View
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 20)
        
        return label
    }()
    
    // MARK: Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .cyan
        
        addSubViews()
        makeLayout()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Override
    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        setNeedsLayout()
        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
        var frame = layoutAttributes.frame
        frame.size = size
        layoutAttributes.frame = frame
        
        return layoutAttributes
    }
}

extension TagCell {
    private func addSubViews() {
        contentView.addSubview(titleLabel)
    }
    
    private func makeLayout() {
        titleLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(contentView).offset(10)
            maker.right.equalTo(contentView).offset(-10)
            maker.top.equalTo(contentView).offset(10)
            maker.bottom.equalTo(contentView).offset(-10)
        }
    }
}

extension TagCell {
    public func configData(_ data: String?) {
        titleLabel.text = data
    }
}
